const app = getApp();
const formatUtil = require('../../utils/formatutil');
const API = require('../../utils/API');

Page({
    data: {
        USERRECORD: wx.getStorageSync('ALLINFO').bbsUserRecord,
        StatusBar: app.globalData.StatusBar,
        CustomBar: app.globalData.CustomBar,

        actionCommentGroups: {},
        showCommentActionsheet: false,

        iconList: [{
            icon: 'noticefill',
            color: 'olive',
            badge: '',
            name: '活动',
            // url: "/pages/components/msg/sysmessage/sysmessage"
            url: "/packageA/activity/activity"

        },

            // {
            //   icon: 'commentfill',
            //   color: 'orange',
            //   badge: 0,
            //   // badge: wx.getStorageSync('ALLINFO').bbsUserRecord.userMessageCount,
            //   name: '行星小商店',
            //   url: "/pages/components/msg/usermessage/usermessage"
            // },
            {
                icon: 'presentfill',
                color: 'red',
                badge: '',
                name: '好吃与好物',
                url: "/pages/components/msg/savemoney/savemoney"
            },],
        skin: true,
        gridBorder: 'no-border',
        UPLOAD_IMAGE: '',
        bbsMessageList: [],

        messageContent: '', //留言内容
        //分页
        pageNo: 1,
        // 触底
        onReachBottomLoading: false,
        //点赞动画
        list: [{
            name: 'scale-down',
            color: 'orange'
        }],
        // 在线人数
        onlinePeopleNumber: 0,
        msgFocus: false,
        showMsgSkeleton: true //显示骨架屏
    },
    onLoad: function () {
        this.getOnlinePeopleNumber()
        // this.towerSwiper('swiperList')
        this.setData({
            UPLOAD_IMAGE: app.globalData.UPLOAD_IMAGE,
            THUMBNAIL: app.globalData.THUMBNAIL,
            USERRECORD: wx.getStorageSync('ALLINFO').bbsUserRecord
        })
        this.waitMessageList()
    },
    onShow() {
        console.log('msg界面显示')
        // 获取用户Record，刷新tabbar提示
        var that = this
        app.getUserAllInfo().then(res => {
            that.setData({
                USERRECORD: res.bbsUserRecord
            })
            app.setTabbarBadge()
        })
        let userMessageCount = wx.getStorageSync('ALLINFO').bbsUserRecord.userMessageCount;
        let userSysMessageCount = wx.getStorageSync('ALLINFO').bbsUserRecord.userSysMessageCount;
        let iconListTmp = this.data.iconList
        iconListTmp[0].badge = userSysMessageCount
        iconListTmp[1].badge = userMessageCount
        this.setData({
            iconList: iconListTmp,
            USERRECORD: wx.getStorageSync('ALLINFO').bbsUserRecord
        })
    },
    onTabItemTap(item) {
        console.log("msg监听tabbar")
        var that = this
        // 获取用户Record，刷新tabbar提示
        app.getUserAllInfo().then(res => {
            that.setData({
                USERRECORD: res.bbsUserRecord
            })
            app.setTabbarBadge()
        })
    },
    // 九宫格start
    showModal(e) {
        console.log(app.globalData.HASUSERINFO)
        var that = this
        const accountState = app.globalData.accountState
        // 查询用户是否授权
        if (app.globalData.HASUSERINFO) {
//           被冻结和禁言都不可以发布
            if (accountState.isDisable) {
                //  被封禁  禁言和冻结都无法发布
                wx.showToast({
                    title: `您的账号已被${accountState.stateName}，无法发布信息,剩余时间${accountState.disableDay}`,
                    icon: 'none'
                })
            } else {
                that.setData({
                    modalName: e.currentTarget.dataset.target,
                    msgFocus: true
                })
            }
        } else {
            app.getUserProfile()
        }
    },
    hideModal(e) {
        this.setData({
            modalName: null
        })
    },

    gridchange: function (e) {
        this.setData({
            gridCol: e.detail.value
        });
    },
    gridswitch: function (e) {
        this.setData({
            gridBorder: e.detail.value
        });
    },
    menuBorder: function (e) {
        this.setData({
            menuBorder: e.detail.value
        });
    },
    menuArrow: function (e) {
        this.setData({
            menuArrow: e.detail.value
        });
    },
    menuCard: function (e) {
        this.setData({
            menuCard: e.detail.value
        });
    },
    switchSex: function (e) {
        this.setData({
            skin: e.detail.value
        });
    },

    //mark:获取留言板列表
    getMessages() {
        const that = this
        let url = `${app.globalData.HOSTURL}/bbs/bbsMessageBoard/wise/mini/fullList?pageNo=${this.data.pageNo}`
        let bbsMessageList = app.wxRequest('get', url, '').then(res => {
            console.log(res)
            if (res.data.code == 200) {
                if (that.data.userPullRefresh) {
                    wx.showToast({
                        title: '刷新成功',
                        icon: 'success'
                    })
                    // that.data.userPullRefresh = false
                    that.setData({
                        userPullRefresh: false
                    })
                }
                if (res.data.result.records.length > 0) {
                    res.data.result.records.forEach((item) => {
                        // 添加动画属性
                        item.exeCuteAnimation = item.userIsPraise
                        item.createTime = formatUtil.showDate(item.createTime)
                        // item.updateTime = formatUtil.showDate(item.updateTime)
                    })
                    //列表追加
                    var tempList = that.data.bbsMessageList
                    for (var item in res.data.result.records) {
                        tempList.push(res.data.result.records[item])
                    }
                    that.setData({
                        bbsMessageList: tempList,
                        getTopicFlag: true
                    })
                    // 获取用户Record，刷新tabbar提示
                    app.getUserAllInfo().then(res => {

                        //TODO 这里应该是更新活动和外卖的图标右上角的点，但是这里却是更新消息，先搁置在这

                        // var userRecord = res.bbsUserRecord
                        // // 设置grid红点
                        // let userMessageCount = userRecord.userMessageCount
                        // let userSysMessageCount = userRecord.userSysMessageCount
                        // let iconListTmp = that.data.iconList
                        // iconListTmp[0].badge = userSysMessageCount
                        // iconListTmp[1].badge = userMessageCount
                        //
                        // that.setData({
                        //     iconList: iconListTmp,
                        //     USERRECORD: userRecord
                        // })
                        app.setTabbarBadge()
                    })
                } else {
                    that.setData({
                        pageNo: that.data.pageNo - 1,
                        getTopicFlag: true
                    })
                }
            } else {
                that.setData({
                    pageNo: 1
                })
            }
            that.setData({
                showMsgSkeleton: false, //隐藏骨架屏
            })
        }, err => {
            that.setData({
                showMsgSkeleton: false, //隐藏骨架屏
            })
        })
        return bbsMessageList
    },

    longpress(e) {
        const that = this

        const User = wx.getStorageSync('ALLINFO').sysUser
        console.log(User)
        const obj = e.currentTarget.dataset.board
        console.log(obj)
        let actionCommentGroupsTemp = [{
            text: '复制',
            value: 0
        }]

        // 自己不能举报自己发布的评论
        if (obj.createBy != User.username) {
            actionCommentGroupsTemp.push({
                text: '举报',
                value: 1
            })
        } else {
            actionCommentGroupsTemp.push({
                text: '删除',
                value: 2
            })
        }


        const userRecord = wx.getStorageSync('ALLINFO').bbsUserRecord
        if (userRecord.roleCodeList.includes("superadmin")) {
            let actionGroupItem = {
                text: '删除（管理员）',
                value: 3
            }
            actionCommentGroupsTemp.push(actionGroupItem);
        }

        that.setData({
            currentBoardObj: obj,
            actionCommentGroups: actionCommentGroupsTemp,
            showCommentActionsheet: true,

        })
    },

    clickCommentAction(e) {
        console.log(e)
        var that = this

        const index = e.detail.value
        console.log(index)
        const currentBoardObj = that.data.currentBoardObj
        console.log(currentBoardObj)
        if (currentBoardObj == undefined || currentBoardObj == null || currentBoardObj == {}) {
            return
        }
        switch (index) {
            //复制
            case 0:
                wx.setClipboardData({
                    data: currentBoardObj.content,
                    success(res) {
                        wx.showToast({
                            title: '复制成功', icon: 'success', duration: 2500,
                        })
                    }
                })
                break
            //举报
            case 1:
                that.tipoffMsg(currentBoardObj)
                break
            //删除
            case 2:
                that.deleteMsg(currentBoardObj.id)
                break
            //    管理员删除
            case 3:
                that.deleteMsg(currentBoardObj.id, true)
                break
        }

        this.setData({
            showCommentActionsheet: false
        })
    },

    deleteMsg: function (id, isAdmin = false) {
        const that = this;

        wx.showModal({
            title: '提示', content: '确认删除该留言吗?', success: function (res) {
                if (res.confirm) {
                    app.wxRequest('delete', app.globalData.HOSTURL + `/bbs/bbsMessageBoard/mini/delete?id=${id}&isadmin=${isAdmin}`,
                        {}).then(res => {
                        console.log(res)
                        if (res.data.code == 200) {
                            wx.showToast({
                                title: '删除成功',
                                icon: 'success',
                                duration: 2000
                            })

                            that.setData({
                                // onReachBottomLoading: false,
                                pageNo: 1,
                                bbsMessageList: [],
                                userPullRefresh: true
                            })
                            //
                            that.getMessages()

                        }
                    })
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })

    },
    tipoffMsg(msgObj) {
        var that = this
        wx.showModal({
            // cancelColor: '#39b54a',
            confirmColor: '#000000',
            title: '举报',
            content: "您确认要举报这条留言吗？",
            showCancel: true,
            success(res) {
                if (res.confirm) {
                    var bbsInform = {}
                    bbsInform.messageId = msgObj.id
                    // bbsInform.saleReplyId = replyDetail.id
                    bbsInform.type = 3 //举报留言
                    bbsInform.informUsername = wx.getStorageSync('ALLINFO').sysUser.username
                    bbsInform.beInformUsername = msgObj.createBy

                    let url = `${app.globalData.HOSTURL}/bbs/bbsInform/wise/mini/informMessage`
                    app.wxRequest('post', url, bbsInform).then(res => {
                        console.log(res)
                        if (res.data.code == 200) {
                            wx.showToast({
                                title: "举报成功",
                                icon: "success"
                            })
                        } else {
                            wx.showToast({
                                title: res.data.message,
                                icon: "none"
                            })
                        }
                    }, err => {

                    })
                } else if (res.cancel) {
                    // x.showToast({
                    //     title: "取消",
                    //     icon: "none"
                    // })  w
                }
            }
        })
    },

    //获取留言板列表-带加载动画
    waitMessageList() {
        var that = this
        var index = 1
        that.getMessages()
        var timer = setInterval(function () {
            if (index % 5 == 0) {
                that.getMessages()
            }
            if (index == 10) {
                that.setData({
                    isLoad: true,
                    onReachBottomLoading: false
                })
                wx.stopPullDownRefresh()
                clearInterval(timer)
            }
            if (that.data.getTopicFlag == true) {
                that.setData({
                    isLoad: true,
                    onReachBottomLoading: true
                })
                wx.stopPullDownRefresh()
                clearInterval(timer)
            }
            index = index + 1;
        }, 1000);
    },
    /**
     * 留言获取
     */
    messageInputContent(e) {
        this.setData({
            messageContent: e.detail.value
        })
    },
    /**
     * 发布留言确认 上传留言
     *
     */
    uploadMessage(e) {
        wx.showLoading({
            title: '正在发布...',
            mask: true
        })
        if (this.data.messageContent.length > 100) {
            wx.showToast({
                title: '留言内容过长，需在100字以内。',
                icon: 'none',
                duration: 2000
            })
        } else if (this.data.messageContent.length < 2) {
            wx.showToast({
                title: '留言内容过短，需大于2个字。',
                icon: 'none',
                duration: 2000
            })
        } else {
            var that = this
            var uploadContent = {
                content: this.data.messageContent
            }
            var bbsMessageList = app.wxRequest(API.message_board_add.method, API.message_board_add.url, uploadContent, (res) => {
                resolve(res.data.result.records)
            }, (err) => {
                reject(err)
            })
            bbsMessageList.then(res => {
                if (res.data.code == 200) {
                    that.hideModal(e)
                    wx.showToast({
                        title: '留言成功',
                        icon: 'success',
                        duration: 2000
                    })

                    that.setData({
                        messageContent: ''
                    })
                    that.data.bbsMessageList = []
                    that.getMessages()
                } else {
                    wx.showToast({
                        title: res.data.message,
                        icon: 'none',
                        duration: 2000
                    })
                }
            })
        }

    },
    onReachBottom: function () {
        this.setData({
            pageNo: this.data.pageNo + 1,
            onReachBottomLoading: false
        })
        this.waitMessageList()
    },
    onPullDownRefresh() {
        // 每次刷新pageNo重置为1
        this.data.bbsMessageList = []
        this.setData({
            onReachBottomLoading: false,
            pageNo: 1,
            bbsMessageList: [],
            userPullRefresh: true
        })
        this.waitMessageList()
        // wx.stopPullDownRefresh()
    },
    //点赞
    clickPraise(e) {
        console.log(e)
        const accountState = app.globalData.accountState
        console.log(this.data.bbsMessageList[e.target.dataset.bindex].userIsPraise)
        //查询用户授权状态
        if (app.globalData.HASUSERINFO) {
            if (accountState.isDisable && accountState.state == accountState.BLOCKING) {
                wx.showToast({
                    title: `您的账号已被${accountState.stateName},剩余时间${accountState.disableDay}`,
                    icon: 'none'
                })
            } else {
                //若果没有点赞  点赞并展示动画
                if (!this.data.bbsMessageList[e.target.dataset.bindex].userIsPraise) {
                    var bbsMessageListTem = this.data.bbsMessageList
                    bbsMessageListTem[e.target.dataset.bindex].userIsPraise = true
                    bbsMessageListTem[e.target.dataset.bindex].exeCuteAnimation = true
                    bbsMessageListTem[e.target.dataset.bindex].praiseCount = bbsMessageListTem[e.target.dataset.bindex].praiseCount + 1
                    this.setData({
                        bbsMessageList: bbsMessageListTem,
                    })
                    let url = API.user_praise_clickPraise.url + '?messageId=' + e.target.id + '&isPraise=' + true
                    app.wxRequest(API.user_praise_clickPraise.method, url, '', (res) => {

                        resolve(res.data.result.records)
                    }, (err) => {
                        reject(err)
                    }).then(res => {
                        app.getUserAllInfo()
                    }, err => {

                    })
                    // 动画
                    // var anmiaton = e.currentTarget.dataset.class;
                    // var that = this;
                    // that.setData({
                    //     animation: anmiaton
                    // })
                    // setTimeout(function () {
                    //     that.setData({
                    //         animation: ''
                    //     })
                    // }, 500)
                } else {
                    var bbsMessageListTem = this.data.bbsMessageList
                    bbsMessageListTem[e.target.dataset.bindex].userIsPraise = false
                    bbsMessageListTem[e.target.dataset.bindex].exeCuteAnimation = false
                    bbsMessageListTem[e.target.dataset.bindex].praiseCount = bbsMessageListTem[e.target.dataset.bindex].praiseCount - 1
                    this.setData({
                        bbsMessageList: bbsMessageListTem,
                    })

                    let url = API.user_praise_clickPraise.url + '?messageId=' + e.target.id + '&isPraise=' + false
                    app.wxRequest(API.user_praise_clickPraise.method, url, '', (res) => {
                        resolve(res.data.result.records)
                    }, (err) => {
                        reject(err)
                    }).then(res => {
                        app.getUserAllInfo()

                    }, err => {

                    })
                }
            }
        } else {
            app.getUserProfile()
        }
    },
    // 点击grid跳转
    gridClick(e) {
        //  || e.currentTarget.dataset.index == 2
        // if (e.currentTarget.dataset.index == 0) {
        //   wx.showToast({
        //     title: '正在开发中...',
        //     icon: 'none'
        //   })
        //   return
        // }
        var that = this
        console.log(e)
        // 清空grid badge
        if (e.currentTarget.dataset.index == 0) {
            // let USERRECORD = wx.getStorageSync('ALLINFO').bbsUserRecord
            // USERRECORD.userSysMessageCount = 0
            // wx.setStorageSync('USERRECORD', USERRECORD)
            // let userMessageCount = wx.getStorageSync('ALLINFO').bbsUserRecord.userMessageCount
            // let userSysMessageCount = wx.getStorageSync('ALLINFO').bbsUserRecord.userSysMessageCount
            // let iconListTmp = this.data.iconList
            // iconListTmp[0].badge = userSysMessageCount
            // this.setData({
            //   iconList: iconListTmp,
            // })
            // if (userMessageCount != 0) {
            //   wx.setTabBarBadge({
            //     index: 1,
            //     text: '' + userMessageCount
            //   })
            // } else {
            //   wx.removeTabBarBadge({
            //     index: 1,
            //     success(res) {
            //       // console.log("移除Badge成功", res)
            //     },
            //     fail(res) {
            //       // console.log("移除Badge失败", res)
            //     }
            //   })
            // }
            // wx.showToast({
            //   title: '正在开发中...',
            //   icon: 'none'
            // })
            // return
        }
        if (e.currentTarget.dataset.index == 1) {
            // let USERRECORD = wx.getStorageSync('ALLINFO').bbsUserRecord
            // USERRECORD.userSysMessageCount = 0
            // wx.setStorageSync('USERRECORD', USERRECORD)
            // let userMessageCount = wx.getStorageSync('ALLINFO').bbsUserRecord.userMessageCount
            // let userSysMessageCount = wx.getStorageSync('ALLINFO').bbsUserRecord.userSysMessageCount
            // let iconListTmp = this.data.iconList
            // iconListTmp[1].badge = userMessageCount
            // this.setData({
            //   iconList: iconListTmp,
            // })
            // if (userSysMessageCount != 0) {
            //   wx.setTabBarBadge({
            //     index: 1,
            //     text: '' + userMessageCount
            //   })
            // } else {
            //   wx.removeTabBarBadge({
            //     index: 1,
            //     success(res) {
            //       // console.log("移除Badge成功", res)
            //     },
            //     fail(res) {
            //       // console.log("移除Badge失败", res)
            //     }
            //   })
            // }
            //   let appId = wx.getStorageSync('ALLINFO').bbsRegion.miniStoreAppid

            //   if (!appId) {
            //     appId = 'wx65d148a2004f8ff0'
            //   }
            //   wx.navigateToMiniProgram({
            //     appId: appId,
            //     path: 'pages/index/index',
            //     extraData: {
            //       foo: 'bar'
            //     },
            //     // envVersion: 'develop',
            //     success(res) {
            //       // 打开成功
            //     }, fail(e) {
            //       if (e.errMsg != "navigateToMiniProgram:fail cancel") {
            //         wx.showToast({
            //           title: '商店打开出错。',
            //           icon: 'none'
            //         })
            //       }
            //     }
            //   })
            //   return
        }
        wx.navigateTo({
            url: that.data.iconList[e.currentTarget.dataset.index].url,
        })
    },
    getOnlinePeopleNumber() {
        var that = this
        let url = app.globalData.HOSTURL + 'bbs/bbsSys/onlinePeopleNumber'
        app.wxRequest('get', url, '').then(res => {
            console.log("在线人数:", res)
            if (res.data.code == 200) {
                console.log(res.data.result)
                that.setData({
                    onlinePeopleNumber: res.data.result
                })
            } else {

            }
        }, err => {

        })
    },
    test() {
        wx.requestSubscribeMessage({
            tmplIds: ['CmA7jHsLU6N5lDzGbpK2vT6vNNhCdV8R4ysqdsFu38w'],
            success(res) {
                console.log(res)
            },
            fail(res) {
                console.log(res)
            }
        })
    },
    // /**
    //  * 用户点击右上角分享
    //  */
    onShareAppMessage: function () {
        let imageUrl = this.data.UPLOAD_IMAGE + wx.getStorageSync('ALLINFO').bbsRegion.regionImage + this.data.THUMBNAIL
        return {
            title: "『" + this.data.USERRECORD.regionFullName + "』" + '都在用的本地小程序',
            path: '/pages/index/index?regionCode=' + this.data.USERRECORD.regionCode,
            // imageUrl: imageUrl
        }
    },
    // 朋友圈
    // onShareTimeline: function () {
    //   return {
    //     title: "『" + this.data.USERRECORD.regionFullName + "』" + '都在用的本地小程序',
    //     path: '/pages/index/index?regionCode=' + this.data.USERRECORD.regionCode,
    //     // 自定义图片路径，可以是本地文件或者网络图片。支持 PNG 及 JPG，显示图片长宽比是 1:1。	默认使用小程序 Logo
    //     // imageUrl:
    //   }
    // },
    goUserInfo(e) {
        console.log(e)
        wx.navigateTo({
            url: '/pages/components/mine/userinfo/userinfo?username=' + e.currentTarget.dataset.username,
        })
    }
})